{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9c9b2e5e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----!"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'HuggingFacePredictor' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-6-f52773c6d675>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     23\u001b[0m predictor = huggingface_model.deploy(\n\u001b[1;32m     24\u001b[0m         \u001b[0minitial_instance_count\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;31m# number of instances\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 25\u001b[0;31m         \u001b[0minstance_type\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'ml.m5.xlarge'\u001b[0m \u001b[0;31m# ec2 instance type\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     26\u001b[0m )\n\u001b[1;32m     27\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/python3/lib/python3.6/site-packages/sagemaker/model.py\u001b[0m in \u001b[0;36mdeploy\u001b[0;34m(self, initial_instance_count, instance_type, serializer, deserializer, accelerator_type, endpoint_name, tags, kms_key, wait, data_capture_config, **kwargs)\u001b[0m\n\u001b[1;32m    735\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    736\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredictor_cls\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 737\u001b[0;31m             \u001b[0mpredictor\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredictor_cls\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mendpoint_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msagemaker_session\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    738\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mserializer\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    739\u001b[0m                 \u001b[0mpredictor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mserializer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mserializer\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'HuggingFacePredictor' object is not callable"
     ]
    }
   ],
   "source": [
    "from sagemaker.huggingface import HuggingFaceModel, HuggingFacePredictor\n",
    "import sagemaker\n",
    "from sagemaker.serializers import NumpySerializer, JSONSerializer\n",
    "\n",
    "role = sagemaker.get_execution_role()\n",
    "# Hub Model configuration. https://huggingface.co/models\n",
    "hub = {\n",
    "\t'HF_MODEL_ID':'google/vit-base-patch16-224',\n",
    "\t'HF_TASK':'image-classification'\n",
    "}\n",
    "\n",
    "# create Hugging Face Model Class\n",
    "huggingface_model = HuggingFaceModel(\n",
    "\ttransformers_version='4.6.1',\n",
    "\tpytorch_version='1.7.1',\n",
    "\tpy_version='py36',\n",
    "\tenv=hub,\n",
    "\trole=role,\n",
    ")\n",
    "\n",
    "# deploy model to SageMaker Inference\n",
    "predictor = huggingface_model.deploy(\n",
    "\tinitial_instance_count=1, # number of instances\n",
    "\tinstance_type='ml.m5.xlarge' # ec2 instance type\n",
    ")\n",
    "\n",
    "# predictor.predict({\n",
    "# \t'inputs': 'https://huggingface.co/datasets/mishig/sample_images/resolve/main/tiger.jpg'\n",
    "# })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a478a3e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# predictor.serializer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a1274ade",
   "metadata": {},
   "outputs": [],
   "source": [
    "# predictor = HuggingFacePredictor(predictor.endpoint_name, serializer=NumpySerializer())\n",
    "# predictor = HuggingFacePredictor('huggingface-pytorch-inference-2021-11-19-09-25-29-400', serializer=NumpySerializer())\n",
    "# predictor = HuggingFacePredictor('huggingface-pytorch-inference-2021-11-19-09-25-29-400', serializer=JSONSerializer())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f0fd785e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# predictor.serializer "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "41430a5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# predictor.predict({\n",
    "# \t'inputs': \"https://huggingface.co/datasets/mishig/sample_images/resolve/main/tiger.jpg\"\n",
    "# })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a43504ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import cv2\n",
    "# image = cv2.imread('1.png')\n",
    "# predictor.predict(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9cfc0984",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import boto3\n",
    "# from botocore.config import Config\n",
    "# from boto3.session import Session\n",
    "\n",
    "# import base64\n",
    "# from PIL import Image\n",
    "# from io import BytesIO\n",
    "\n",
    "# image = Image.open(\"1.png\").convert('RGB')\n",
    "# buffered = BytesIO()\n",
    "# image.save(buffered, format=\"JPEG\")\n",
    "# # payload = str(base64.b64encode(buffered.getvalue()))\n",
    "# payload = buffered.getvalue()\n",
    "# print(type(payload))\n",
    "\n",
    "# print(predictor.endpoint_name)\n",
    "# # predictor.predict(payload)\n",
    "\n",
    "# config = Config(\n",
    "#     read_timeout=120,\n",
    "#     retries={\n",
    "#         'max_attempts': 0\n",
    "#     }\n",
    "# )\n",
    "\n",
    "# sagemaker_runtime_client = boto3.client('sagemaker-runtime', config=config)\n",
    "# session = Session(sagemaker_runtime_client)\n",
    "\n",
    "# response = sagemaker_runtime_client.invoke_endpoint(\n",
    "#     EndpointName='huggingface-pytorch-inference-2021-11-19-09-25-29-400',  # predictor.endpoint_name,\n",
    "#     ContentType=\"application/x-npy\",\n",
    "#     Body=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18f19ab2",
   "metadata": {},
   "outputs": [],
   "source": [
    "predictor.delete()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
